//<<<<<<< HEAD
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hitechbangla.fctu.form;

import com.hitechbangla.fctu.base.GameScene;
import com.hitechbangla.fctu.base.Location;
import com.hitechbangla.fctu.base.radar.*;
import com.hitechbangla.fctu.utility.GeneralUtil;
import com.hitechbangla.fctu.utility.RecordedGame;
import fctu.simulator.HTB_MAIN;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.geom.Point2D;
import org.openstreetmap.gui.jmapviewer.OsmMercator;

/**
 *
 * @author Asus
 */
public class AddRadar extends javax.swing.JDialog {

    /**
     * Creates new form AddRadar
     */
    //HTB_MAIN HTBParent;
    GameScene gameScene;
    String[] radarTypeString;
    Point2D point;

    private double lon;
    private double lat;
    
    public AddRadar(GameScene gameScene, Point2D point) {
	//initComponents();
	this.gameScene = gameScene;
	this.point = point;
        this.lon = OsmMercator.XToLon((int)(point.getX() + HTB_MAIN.translate_x), HTB_MAIN.currentZoom);
        this.lat = OsmMercator.YToLat((int)(-(point.getY() - HTB_MAIN.translate_y)), HTB_MAIN.currentZoom);

	this.setLocation(MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y);
	//this.HTBParent = HTBParent;
	
	radarTypeString = new String[gameScene.radarTypeList.size()];
	for (int i = 0; i < radarTypeString.length; i++) {
	    radarTypeString[i] = gameScene.radarTypeList.get(i).getRadarType();
	}//
	initComponents();
	setModalityType(ModalityType.APPLICATION_MODAL);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    private void myInitComponent() {

	// private void initComponents() {
	//System.out.println(gameScene.radarTypeList.size() + "size is ");
	setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
	setTitle("Ad Radar");


	/*
	 jPanel1Layout.setHorizontalGroup(
	 jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	 .addGroup(jPanel1Layout.createSequentialGroup()
	 .addContainerGap()
	 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING));
	 */




	/*
	 jScrollPane1 = new javax.swing.JScrollPane();
	 jPanel1 = new javax.swing.JPanel();
	 jButton1 = new javax.swing.JButton();
	 jRadioButton1 = new javax.swing.JRadioButton();
	 jRadioButton2 = new javax.swing.JRadioButton();
	 jRadioButton3 = new javax.swing.JRadioButton();
	 jRadioButton4 = new javax.swing.JRadioButton();
	 jRadioButton5 = new javax.swing.JRadioButton();
	 jRadioButton6 = new javax.swing.JRadioButton();
	 jRadioButton7 = new javax.swing.JRadioButton();
	 jRadioButton8 = new javax.swing.JRadioButton();
	 jRadioButton9 = new javax.swing.JRadioButton();

	 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
	 setTitle("Ad Radar");

	 jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

	 jButton1.setText("Ok");

	 jRadioButton1.setText("jRadioButton1");
	 jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
	 public void actionPerformed(java.awt.event.ActionEvent evt) {
	 jRadioButton1ActionPerformed(evt);
	 }
	 });

	 jRadioButton2.setText("jRadioButton1");

	 jRadioButton3.setText("jRadioButton1");

	 jRadioButton4.setText("jRadioButton1");

	 jRadioButton5.setText("jRadioButton1");

	 jRadioButton6.setText("jRadioButton1");

	 jRadioButton7.setText("jRadioButton1");

	 jRadioButton8.setText("jRadioButton1");

	 jRadioButton9.setText("jRadioButton1");

	 javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
	 jPanel1.setLayout(jPanel1Layout);
	 jPanel1Layout.setHorizontalGroup(
	 jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	 .addGroup(jPanel1Layout.createSequentialGroup()
	 .addContainerGap()
	 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	 .addComponent(jRadioButton9)
	 .addComponent(jRadioButton5)
	 .addComponent(jRadioButton6)
	 .addComponent(jRadioButton7)
	 .addComponent(jRadioButton8)
	 .addComponent(jRadioButton3)
	 .addComponent(jRadioButton4)
	 .addComponent(jRadioButton2)
	 .addComponent(jButton1)
	 .addComponent(jRadioButton1))
	 .addContainerGap(97, Short.MAX_VALUE))
	 );
	 jPanel1Layout.setVerticalGroup(
	 jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	 .addGroup(jPanel1Layout.createSequentialGroup()
	 .addGap(15, 15, 15)
	 .addComponent(jRadioButton1)
	 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	 .addComponent(jRadioButton2)
	 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	 .addComponent(jRadioButton4)
	 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	 .addComponent(jRadioButton3)
	 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	 .addComponent(jRadioButton8)
	 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	 .addComponent(jRadioButton7)
	 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	 .addComponent(jRadioButton6)
	 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	 .addComponent(jRadioButton5)
	 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	 .addComponent(jRadioButton9)
	 .addGap(7, 7, 7)
	 .addComponent(jButton1)
	 .addContainerGap(25, Short.MAX_VALUE))
	 );

	 jScrollPane1.setViewportView(jPanel1);

	 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	 getContentPane().setLayout(layout);
	 layout.setHorizontalGroup(
	 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
	 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
	 .addContainerGap())
	 );
	 layout.setVerticalGroup(
	 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	 .addGroup(layout.createSequentialGroup()
	 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
	 .addGap(0, 19, Short.MAX_VALUE))
	 );

	 pack();
	 */

    }//end of init components

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jComboBoxAcType = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldX = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldY = new javax.swing.JTextField();
        jButtonOK = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldRadarName = new javax.swing.JTextField();

        setTitle("Add Radar");
        setAlwaysOnTop(true);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setName("frameAddRadar"); // NOI18N
        setResizable(false);

        if(radarTypeString != null){
            jComboBoxAcType.setModel(new javax.swing.DefaultComboBoxModel(radarTypeString));
        }

        jLabel1.setText("Select Aircraft Type:");

        jTextFieldX.setText(String.valueOf(this.lon));
        jTextFieldX.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldXActionPerformed(evt);
            }
        });

        jLabel2.setText("X-coordinate:");

        jLabel3.setText("Y-coordinate:");

        jTextFieldY.setText(String.valueOf(this.lat));

        jButtonOK.setText("OK");
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jLabel4.setText("Radar Name:");

        jTextFieldRadarName.setText("");
        jTextFieldRadarName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldRadarNameActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldX, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextFieldY, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jComboBoxAcType, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jTextFieldRadarName, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(jButtonOK)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonCancel)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldRadarName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxAcType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonOK)
                    .addComponent(jButtonCancel))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel1.getAccessibleContext().setAccessibleName("Add Radar");

        getAccessibleContext().setAccessibleName("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldXActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldXActionPerformed
	// TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldXActionPerformed

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
	int i = jComboBoxAcType.getSelectedIndex();
	//System.out.println("" + i);
	RadarType radarType = gameScene.radarTypeList.get(i);
        
        double fLon = Double.parseDouble(jTextFieldX.getText());
            double fLat = Double.parseDouble(jTextFieldY.getText());
            
            int fX = OsmMercator.LonToX(fLon, HTB_MAIN.currentZoom);
            int fY = OsmMercator.LatToY(fLat, HTB_MAIN.currentZoom);
            
            double x = (fX - HTB_MAIN.translate_x);
	    double y = (-fY + HTB_MAIN.translate_y);
            
        double nm_system_x = (x) * gameScene.HTBParent.one_nm_equivalent_pixel;
        double nm_system_y = (y) * gameScene.HTBParent.one_nm_equivalent_pixel;
        String radarNM= jTextFieldRadarName.getText();
	Location radLocation = new Location(nm_system_x, nm_system_y, 0);
	RadarState radState = new RadarState(radLocation,radarNM);
	Radar radar = new Radar(radarType, radState, gameScene.airCraftList);
	radar.addHTBParent(gameScene.HTBParent);
	gameScene.radarList.add(radar);
	//gameScene.radarList.add(radar);
	this.setVisible(false);
        GeneralUtil.UpdateGameScene(gameScene);
	
	RecordedGame recordedGame = gameScene.HTBParent.getRecordedGame();
	
	if(recordedGame!=null)
	{
	    if(recordedGame.isRecordingStarted())
	    {
	        recordedGame.storeGameScene(gameScene);
	    }
	}
	
    }//GEN-LAST:event_jButtonOKActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
	this.setVisible(false);
	dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jTextFieldRadarNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldRadarNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldRadarNameActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
	/* Set the Nimbus look and feel */
	//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
	 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
	 */
	try {
	    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
		if ("Nimbus".equals(info.getName())) {
		    javax.swing.UIManager.setLookAndFeel(info.getClassName());
		    break;
		}
	    }
	} catch (ClassNotFoundException ex) {
	    java.util.logging.Logger.getLogger(AddRadar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (InstantiationException ex) {
	    java.util.logging.Logger.getLogger(AddRadar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (IllegalAccessException ex) {
	    java.util.logging.Logger.getLogger(AddRadar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (javax.swing.UnsupportedLookAndFeelException ex) {
	    java.util.logging.Logger.getLogger(AddRadar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	}
	//</editor-fold>

	/* Create and display the form */
	java.awt.EventQueue.invokeLater(new Runnable() {
	    public void run() {
		//new AddRadar(null, null).setVisible(true);
	    }
	});
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JComboBox jComboBoxAcType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    public javax.swing.JTextField jTextFieldRadarName;
    private javax.swing.JTextField jTextFieldX;
    private javax.swing.JTextField jTextFieldY;
    // End of variables declaration//GEN-END:variables
}

